﻿package com.teaphy.coroutinecrawler.build_coroutine

import kotlinx.coroutines.*

fun main() = runBlocking {
    println("start")

    println("coroutineScope -- runBlocking: $this")

    val job1 = launch(CoroutineName("AAA")) {
        println("coroutineScope -- launch: $this")
        // 创建一个CoroutineScope
        val result = coroutineScope {

            println("coroutineScope -- coroutineScope: $this")

            delay(3000)

            200
        }

        println("coroutineScope result: $result")
    }

    launch {
        coroutineScope {
            println("coroutineScope -- supervisorScope2: $this")

            // 子协程的失败不会导致此CoroutineScope失败，也不会影响其他子协程
            launch {
                delay(1000)
                throw Exception("1111")
            }

            launch {
                println("coroutineScope -- supervisorScope2 - launch2 --> start")
                delay(2000)
                println("coroutineScope -- supervisorScope2 - launch2 --> end")
            }
        }

    }

//    launch {
//        delay(2000)
//        job1.cancel()
//    }

    // 等待其协程体以及所有子协程结束。
    println("Coroutine scope is Over")
    println("end")
}